package kr.hkit.androidcomplete02.ch16;

import kr.hkit.androidcomplete02.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomDialog extends Activity implements CustomDialogFragment.NoticeDialogListener{
	TextView tv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch16_dialogtest);
		tv = (TextView) findViewById(R.id.p_text);
	}
	
	public void mOnClick(View v){
		Bundle bundle = new Bundle();
		bundle.putString("title", "주문정보를 입력하시오");

		CustomDialogFragment listFrg = new CustomDialogFragment();		
		listFrg.setArguments(bundle);
		listFrg.show(getFragmentManager(), "dialog");	
		
	}

	@Override
	public void onDialogClick(DialogFragment frg, String res) {
		tv.setText("선택한 사항은 : " + res);
	}
}

class CustomDialogFragment extends DialogFragment{
	NoticeDialogListener mListener;
	Bundle bundle = new Bundle();
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		
		try{
		mListener = (NoticeDialogListener) activity;
		}catch(ClassCastException e){
			Log.d("ClassCastException", activity.toString() + "ListDialogFragment을 구현");
		}
	}
	
	public interface NoticeDialogListener{
		public void onDialogClick(DialogFragment frg, String res);
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		bundle = getArguments();
		LayoutInflater inflater = getActivity().getLayoutInflater();
		final LinearLayout linear = (LinearLayout) inflater.inflate(
				R.layout.order, null);

		return new AlertDialog.Builder(getActivity())
				.setIcon(R.drawable.ic_launcher)
				.setTitle(bundle.getString("title"))
				.setView(linear)
				.setPositiveButton("확인", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						EditText product = (EditText) linear
								.findViewById(R.id.product);
						EditText number = (EditText) linear
								.findViewById(R.id.number);
						CheckBox paymethod = (CheckBox) linear
								.findViewById(R.id.paymethod);
						StringBuilder sb = new StringBuilder("주문 정보\n");
						sb.append(product.getText() + " ");
						sb.append(number.getText() + "개 ");
						sb.append(paymethod.isChecked() ? "착불결제" : "");
						mListener.onDialogClick(CustomDialogFragment.this,
								sb.toString());
					}
				}).setNegativeButton("취소", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						mListener.onDialogClick(CustomDialogFragment.this,
								"주문을 취소했습니다.");
					}
				}).create();
	}
}

